//
//  ItemDetailViewController.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/20.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "ItemDetailViewController.h"
#import "DisplayUtils.h"
#import "OrderManager.h"
#import "AppDelegate.h"
#import <QuartzCore/QuartzCore.h>

@interface ItemDetailViewController ()

@end

@implementation ItemDetailViewController
@synthesize item = _item;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    CGSize nameLabelSize = [_item.name sizeWithFont:_nameLabel.font
                                  constrainedToSize:CGSizeMake( _nameLabel.frame.size.width, 1000)
                                      lineBreakMode:_nameLabel.lineBreakMode];
    _nameLabel.frame = CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y,
                                  _nameLabel.frame.size.width, nameLabelSize.height);
    _nameLabel.text = _item.name;
    CGSize commentLabelSize = [_item.comment sizeWithFont:_commentLabel.font
                                        constrainedToSize:CGSizeMake( _commentLabel.frame.size.width, 1000)
                                            lineBreakMode:_commentLabel.lineBreakMode];
    _commentLabel.frame = CGRectMake(_commentLabel.frame.origin.x, _commentLabel.frame.origin.y,
                                     _commentLabel.frame.size.width, commentLabelSize.height);
    _commentLabel.text = _item.comment;
    _priceLabel.text = [DisplayUtils displayStringByDecimalNumber:_item.price];
    
    NSInteger imageScrollViewPage = 0;
    if (!_item.image.image1_path) {
        imageScrollViewPage++;
        [self setImageViewOnImageScrollView:_item.image.image1_path page:imageScrollViewPage];
    }
    if (_item.image.image2_path) {
        imageScrollViewPage++;
        [self setImageViewOnImageScrollView:_item.image.image2_path page:imageScrollViewPage];
    }
    if (_item.image.image3_path) {
        imageScrollViewPage++;
        [self setImageViewOnImageScrollView:_item.image.image3_path page:imageScrollViewPage];
    }

    _numberOfItemsLabel.text = [NSString stringWithFormat:@"%d", 1];
    [self calculateTotalPrice:[NSDecimalNumber decimalNumberWithString:_numberOfItemsLabel.text]];
}

- (void)setImageViewOnImageScrollView:(NSString *)imagePath page:(NSInteger)page
{
    [_imageScrollView setContentSize:CGSizeMake(_imageScrollView.frame.size.width*page,
                                                _imageScrollView.frame.size.height)];
    CGSize scrollViewSize = _imageScrollView.frame.size;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollViewSize.width*(page-1), 0,
                                                                           scrollViewSize.width,
                                                                           scrollViewSize.height)];
    NSLog(@">>>page:%d", page);
    NSLog(@">>>origin:%f", scrollViewSize.width*page);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"yakitori1.jpg"];
    [_imageScrollView addSubview:imageView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)selectCancel:(id)sender
{
    [self dismissViewControllerAnimated:TRUE completion:nil];
}

- (IBAction)selectDecision:(id)sender
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    Order *order = [OrderManager newRecord:appDelegate.managedObjectContext];
    order.item = _item;
    order.date = [NSDate date];
    order.is_done = [NSNumber numberWithBool:FALSE];
    order.number_of_item = [NSNumber numberWithInteger:[_numberOfItemsLabel.text integerValue]];
    [appDelegate saveContext];
    
    [self dismissViewControllerAnimated:TRUE completion:nil];
}

- (IBAction)selectCountUp:(id)sender
{
    NSInteger itemCnt = [_numberOfItemsLabel.text integerValue]+1;
    if (99 < itemCnt) {
        return;
    }
    if (1 <= itemCnt) {
        [_decisionBarButton setEnabled:TRUE];
    }
    _numberOfItemsLabel.text = [NSString stringWithFormat:@"%d", itemCnt];
    [self calculateTotalPrice:[NSDecimalNumber decimalNumberWithString:_numberOfItemsLabel.text]];
}

- (IBAction)selectCountDown:(id)sender
{
    NSInteger itemCnt = [_numberOfItemsLabel.text integerValue]-1;
    if (itemCnt < 0) {
        return;
    }
    if (itemCnt == 0) {
        [_decisionBarButton setEnabled:FALSE];
    }
    _numberOfItemsLabel.text = [NSString stringWithFormat:@"%d", itemCnt];
    [self calculateTotalPrice:[NSDecimalNumber decimalNumberWithString:_numberOfItemsLabel.text]];
}

- (void)calculateTotalPrice:(NSDecimalNumber*)numberOfItems
{
    NSDecimalNumber *number = [_item.price decimalNumberByMultiplyingBy:numberOfItems];
    _priceLabel.text = [DisplayUtils displayStringByDecimalNumber:number];
}
@end
